package com.kdtech.analyse.news;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class ChinajilinNewsAnalyse implements AnalyseNews {

	//http://www.chinajilin.com.cn/content/2013-05/07/content_2853544.htm
	//http://auto.chinajilin.com.cn/news/story_943062.html
	private static final String[] regex={
		"http://www.chinajilin.com.cn/content/[0-9]*-[0-9]*/[0-9]*/content_[0-9]*.htm",
		"http://auto.chinajilin.com.cn/news/story_[0-9]*.html",
		"http://digi.chinajilin.com.cn/[0-9]*/[0-9]*b.shtml",
		"http://www.chinajilin.com.cn/.*/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
		"http://news.chinajilin.com.cn/.*/[0-9]*/[0-9]*.html"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();
		//news=(NewsMeta) new ParseNewsUtils().fixed(urlMeta, news);

		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
			news=new NewsMeta();
			String htmltxt=urlMeta.getHtml();
			Document doc=Jsoup.parse(htmltxt);
			String title=null;
			String content=null;
			Long date=null;
			title=doc.select("span.style4").text();
			content=JSoupUtils.selectContent(url, doc,"div.zhengwen");
			date=DateUtils.matchDate(doc.select("span.z3").text());
			news.setUrl(url);
			news.setTitle(title);
			news.setContent(content);
			news.setDate(date);
			String author=null;
			/*
			 * 作者
			 */
			author=JSoupUtils.matchAuthor(doc, "来源:");
			news.setAuthor(author);
		}
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://news.chinajilin.com.cn/gnyw/201611/2254926.html";
		ChinajilinNewsAnalyse test=new ChinajilinNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		
		
		System.out.println(test.isDetailPage(url));
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
